function getUrl(){
    var url = window.location.href;
    var path = window.location.pathname;
    var search = window.location.search;
    url = url.replace(path,"/");
    url = url.replace(search,"")
    return url;
}

$(document).ready(function () {
    const url = getUrl();

    $.ajax({
        url:url+"adminInfo/getCheck",
        data:{},
        type: "get",
        contentType: "application/json",
        success: function(map) {
            console.log(map);
            $("#num").text("当前节点高度："+map.num);

            if(!($.isEmptyObject(map.ws))){
                $.each(map.ws, function (i, wsAdr) {
                    console.log(wsAdr);
    
                    const option = '<option>'+wsAdr+'</option>';
    
                    const $obj2 = $(option);
    
                    $("#peerSelect").append($obj2);
                })
            }
            
        },
        error:function(){
            console.log("----------")
        }
    })
})

$("#check").click(function () {
    const url = getUrl();
    const start = $("#start").val();
    const end = $("#end").val();

    const data ={
        "start":start,
        "end":end
    }


    $.ajax({
        url:url+"adminInfo/checkBlock",
        data:JSON.stringify(data),
        type: "post",
        contentType: "application/json; charset=utf-8",
        success: function(result) {
            console.log(result);
            if(result==-1){
                $("#result").text("无异常");
                $("#result").css("color","blue");
            }else{
                $("#result").text("第"+result+"区块被篡改");
                $("#result").css("color","red");
            }
        },
        error:function(){
            console.log("----------")
        }
    })
})

$("#selectPeer").click(function () {
    const url = getUrl();
    const start = $("#block_start").val();
    const end = $("#block_end").val();
    const peer = $("#peerSelect").val();

    const data ={
        "start":start,
        "end":end,
        "peer":peer
    }


    $.ajax({
        url:url+"adminInfo/getDataFromPeer",
        data:JSON.stringify(data),
        type: "post",
        contentType: "application/json; charset=utf-8",
        success: function(result) {
            console.log(result);
            
        },
        error:function(){
            console.log("----------")
        }
    })
})